//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.Common;
using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
using System.Windows.Input;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.WorkflowManagerModule
{
    /// <summary>
    /// Interaction logic for AvailableWorkflowsListView.xaml
    /// </summary>
    public sealed partial class AvailableWorkflowsListView : UserControl
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public AvailableWorkflowsListView()
        {
            InitializeComponent();
        }

        void DisplayListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView item = sender as ListView;
            if (item != null)
            {
                WorkflowViewModel workflowModel = item.SelectedItem as WorkflowViewModel;

                if (workflowModel != null)
                {
                    (this.DataContext as AvailableWorkflowsListPresenter).WorkflowSelectChangedCommand.Execute(workflowModel);
                }
            }
        }

        /// <summary>
        /// The event handler for the context menu opening event on the listview item.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            IListPresenter listPresenter = this.DataContext as IListPresenter;
            ListViewItem selectedItem = sender as ListViewItem;

            ContextMenu contextMenu = selectedItem.ContextMenu;
            object workflowModel = selectedItem.DataContext;

            // Add the menu items present in the presenter to the context menu.
            foreach (MenuItem menuItem in listPresenter.MenuItemCollection)
            {
                ContextMenu parent = menuItem.Parent as ContextMenu;
                if (parent != null)
                {
                    parent.Items.Remove(menuItem);
                }
                menuItem.CommandParameter = workflowModel;
                contextMenu.Items.Add(menuItem);
            }
        }

        /// <summary>
        /// The event handler for the toggle button checked event.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            ToggleButton button = sender as ToggleButton;
            ListViewItem listViewItem = this.DisplayListView.ContainerFromElement(button) as ListViewItem;
            (this.DataContext as AvailableWorkflowsListPresenter).JobDetailExpanded.Execute(listViewItem);
        }

        /// <summary>
        /// The event handler for the toggle button unchecked event.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            ToggleButton button = sender as ToggleButton;
            ListViewItem listViewItem = this.DisplayListView.ContainerFromElement(button) as ListViewItem;
            (this.DataContext as AvailableWorkflowsListPresenter).JobDetailCollapsed.Execute(listViewItem);
        }

        /// <summary>
        /// Selects the listview items even if any of the items inside are clicked.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void PreviewListViewItemClicked(object sender, RoutedEventArgs e)
        {
            ListViewItem item = sender as ListViewItem;
            this.DisplayListView.SelectedItem = item.DataContext;

            WorkflowViewModel workflowModel = (e.OriginalSource as FrameworkElement).DataContext as WorkflowViewModel;
            if (workflowModel != null)
            {
                (this.DataContext as AvailableWorkflowsListPresenter).WorkflowSelectedCommand.Execute(true);
            }
            else
            {
                (this.DataContext as AvailableWorkflowsListPresenter).WorkflowSelectedCommand.Execute(false);
            }
        }

        /// <summary>
        /// Event handler for the double click event.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void ListViewItemDoubleClicked(object sender, RoutedEventArgs e)
        {
            ListViewItem item = sender as ListViewItem;
            this.DisplayListView.SelectedItem = item.DataContext;

            // Invoke the trident composer on double click.
            (this.DataContext as AvailableWorkflowsListPresenter).EditWorkflowCommand.Execute(item.DataContext as WorkflowViewModel);
        }


        /// <summary>
        /// Event handler for the delete key press event.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void DisplayListView_KeyDown(object sender, KeyEventArgs e)
        {
            WorkflowViewModel model = this.DisplayListView.SelectedItem as WorkflowViewModel;
            if (e.Key == Key.Delete)
            {
                if (model != null)
                {
                    if ((this.DataContext as AvailableWorkflowsListPresenter).DeleteWorkflowCommand.CanExecute(model))
                    {
                        (this.DataContext as AvailableWorkflowsListPresenter).DeleteWorkflowCommand.Execute(model);
                    }
                }
            }
            // If key events to handle increase introduce a switch statement.
        }

    }
}
